import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Class ServerInterface create all graphical component that will compose the interface
 * @author  jonathan MEISER
 */
public class ServerInterface extends JFrame 
{
	/**
	 * @uml.property  name="head"
	 */
	private static String[] head = {"Liste des utitilisateurs"};
	
	/**
	 * @uml.property  name="screenSize"
	 */
	private Dimension screenSize;
	/**
	 * @uml.property  name="server"
	 * @uml.associationEnd  
	 */
	private ChatServer server;

	/**
	 * @uml.property  name="start"
	 */
	private JButton start;
	/**
	 * @uml.property  name="stop"
	 */
	private JButton stop;
	/**
	 * @uml.property  name="log"
	 */
	private JButton log;
	/**
	 * @uml.property  name="administration"
	 * @uml.associationEnd  
	 */
	private ButtonAdministration administration;
	
	/**
	 * @uml.property  name="button"
	 */
	private JPanel button;
	
	/**
	 * @uml.property  name="users"
	 */
	protected JPanel users;	
	/**
	 * @uml.property  name="messages"
	 */
	private JPanel messages;

	 /**
	  * Constructor of the class
	  * initialize all graphic component
	  */
	 public ServerInterface(int port)
	 {
	    screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	    server = new ChatServer(port);
	   
	    try
        {
	    	//JFrame properties
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setLocale(java.util.Locale.getDefault());
			this.setResizable(true);
			this.setTitle("Chat - Server administration");
			this.setSize(screenSize.width, screenSize.height);
			this.setLocation( 0 , 0 );
			this.setLayout(null);
			this.setExtendedState(JFrame.MAXIMIZED_BOTH);
			
			administration = new ButtonAdministration(this);
			start = new JButton ();
			start.setName("start");
			start.setPreferredSize(new Dimension(100, 50));
			start.addActionListener(administration);
			start.setIcon(new ImageIcon(this.getClass().getResource("start.png")));
			start.setRolloverIcon(new ImageIcon(this.getClass().getResource("start_over.png")));
			start.setDisabledIcon(new ImageIcon(this.getClass().getResource("start_disabled.png")));
			start.setFocusPainted(false);
			start.setBorderPainted(false);
			start.setContentAreaFilled(false);
			start.setRolloverEnabled(true);
			stop = new JButton ();
			stop.setName("stop");
			stop.setPreferredSize(new Dimension(100, 50));
			stop.addActionListener(administration);
			stop.setEnabled(false);
			stop.setIcon(new ImageIcon(this.getClass().getResource("stop.png")));
			stop.setRolloverIcon(new ImageIcon(this.getClass().getResource("stop_over.png")));
			stop.setDisabledIcon(new ImageIcon(this.getClass().getResource("stop_disabled.png")));
			stop.setFocusPainted(false);
			stop.setBorderPainted(false);
			stop.setContentAreaFilled(false);
			stop.setRolloverEnabled(true);
			log = new JButton ();
			log.setName("log");
			log.setPreferredSize(new Dimension(100, 50));
			log.addActionListener(administration);
			log.setIcon(new ImageIcon(this.getClass().getResource("log.png")));
			log.setRolloverIcon(new ImageIcon(this.getClass().getResource("log_over.png")));
			log.setFocusPainted(false);
			log.setBorderPainted(false);
			log.setContentAreaFilled(false);
			log.setRolloverEnabled(true);
			
			users = new JPanel(new FlowLayout());
			users.setBounds(0, 60, 170, this.getScreenSize().height-88);
			messages = new JPanel(new FlowLayout());
			messages.setBounds(170, 60, this.getScreenSize().width-170, this.getScreenSize().height-88);
			
			button = new JPanel(new FlowLayout());
			button.setBounds(0, 0, screenSize.width, 60);
			button.add(start);
			button.add(stop);
			button.add(log);	
			
			this.add(button);
			this.add(users);
			this.add(messages);
        }
	    catch (Exception e)
        {
            System.err.println("<ServerInterface> - Error while creating interface : " + e);
            e.printStackTrace();
        }
	 }
	 
	/**
     * calculate a width according to a percentage of the screen
     * @param percent  Percentage of the screen
     * @return the calculated width
     */
    public int getWidth(int percent)
    {
    	percent = percent / 10;
    	return ( percent * (screenSize.width/10) );
    }
    
    /**
     * calculate a height according to a percentage of the screen
     * @param percent  Percentage of the screen
     * @return the calculated height
     */
    public int getHeight(int percent)
    {
    	percent = percent / 10;
    	return ( percent * (screenSize.height/10) );
    }
    
    //GETTER AND SETTER***********************************************************************
    /**
	 * @return
	 * @uml.property  name="head"
	 */
    public static String[] getHead() {
		return head;
	}

	/**
	 * @param head
	 * @uml.property  name="head"
	 */
	public static void setHead(String[] head) {
		ServerInterface.head = head;
	}

	/**
	 * @return
	 * @uml.property  name="screenSize"
	 */
	public Dimension getScreenSize() {
		return screenSize;
	}

	/**
	 * @param screenSize
	 * @uml.property  name="screenSize"
	 */
	public void setScreenSize(Dimension screenSize) {
		this.screenSize = screenSize;
	}

	/**
	 * @return
	 * @uml.property  name="server"
	 */
	public ChatServer getServer() {
		return server;
	}

	/**
	 * @param server
	 * @uml.property  name="server"
	 */
	public void setServer(ChatServer server) {
		this.server = server;
	}

	/**
	 * @return
	 * @uml.property  name="start"
	 */
	public JButton getStart() {
		return start;
	}

	/**
	 * @param start
	 * @uml.property  name="start"
	 */
	public void setStart(JButton start) {
		this.start = start;
	}

	/**
	 * @return
	 * @uml.property  name="stop"
	 */
	public JButton getStop() {
		return stop;
	}

	/**
	 * @param stop
	 * @uml.property  name="stop"
	 */
	public void setStop(JButton stop) {
		this.stop = stop;
	}

	/**
	 * @return
	 * @uml.property  name="log"
	 */
	public JButton getLog() {
		return log;
	}

	/**
	 * @param log
	 * @uml.property  name="log"
	 */
	public void setLog(JButton log) {
		this.log = log;
	}

	/**
	 * @return
	 * @uml.property  name="button"
	 */
	public JPanel getButton() {
		return button;
	}

	/**
	 * @param button
	 * @uml.property  name="button"
	 */
	public void setButton(JPanel button) {
		this.button = button;
	}

	/**
	 * @return
	 * @uml.property  name="users"
	 */
	public JPanel getUsers() {
		return users;
	}

	/**
	 * @param user
	 * @uml.property  name="users"
	 */
	public void setUsers(JPanel user) {
		this.users = user;
	}

	/**
	 * @return
	 * @uml.property  name="messages"
	 */
	public JPanel getMessages() {
		return messages;
	}

	/**
	 * @param messages
	 * @uml.property  name="messages"
	 */
	public void setMessages(JPanel messages) {
		this.messages = messages;
	}
	 
	 
	/**
	 * Main method
	 * @param args   contain the port number
	 * If the port number is null, the server will be start of a default port number
	 */
	 public static void main(String[] args) 
	    {
	    	try
	    	{
	    		int port = 5555;
	    		if (args.length > 0)
	    		{
	    			try
	    			{
	    				port = Integer.parseInt(args[0]); 
	    			}
	    			catch (Exception e)
	    	    	{
	    	    		System.err.println("<Main> - Argument error : " + e);
	    	    		System.err.println("Server will be start of default port 5555");
	    	            e.printStackTrace();
	    	            port = 5555;
	    	    	}
	    		}
		    	ServerInterface si = new ServerInterface(port);
		    	si.setVisible(true);
		    	Refresh t = new Refresh(si);
				t.start();
	    	}
	    	catch (Exception e)
	    	{
	    		System.err.println("<Main> - An Error is occured : " + e);
	            e.printStackTrace();
	    	}
	    }
}
